//
//  ChannelManageViewController.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/4.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "ChannelManageViewController.h"
#import "GameClassifyModel.h"
#import "SortCollectionViewCell.h"
#import "SortCollectionReusableView.h"
#import "SortBasicCollectionCellLayout.h"

static NSString * const kSortCollectionCell   = @"kSortCollectionCell";
static NSString * const kSortHeadReusableView = @"kSortHeadReusableView";

@interface ChannelManageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,SortBasicCollectionCellLayoutDelegate>
@property (nonatomic, strong) UIButton *manageButton;
@property (nonatomic, assign) BOOL isSelectManageButton;
@property (nonatomic, strong) UICollectionView *sortCollectionView;
@property (nonatomic, strong) SortBasicCollectionCellLayout *collectionViewLayout;
@property (nonatomic, strong) NSMutableArray *commonArray;
@property (nonatomic, strong) NSMutableArray *moreArray;

@end

@implementation ChannelManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"频道管理";
    
    //隐藏返回按钮
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = nil;
    
    
    
    
    
    NSArray *titleArray = @[@"斗罗大陆",@"乡村教师",@"唐门暗器",@"斗破苍窘",@"主播联萌",@"将夜",@"大魏宫廷",@"净瓶梅",@"极品女上司",@"我的美女上司",@"盗墓笔记",@"阴阳师"];
    
    for (int i = 0; i < titleArray.count; i++) {
        GameClassifyModel *model = [GameClassifyModel new];
        model.gameName = [titleArray objectAtIndex:i];
        [self.commonArray addObject:model];
    }
    
    NSArray *moreArray = @[@"易大师",@"开海",@"战国志军",@"三国演义",@"十万个为什么",@"奥术起源",@"梦回大唐"];
    
    for (int i = 0; i < moreArray.count; i++) {
        GameClassifyModel *model = [GameClassifyModel new];
        model.gameName = [moreArray objectAtIndex:i];
        [self.moreArray addObject:model];
    }
    
    [self setUpTheViewFrame];
    
    //设置成可编辑状态
    [self manageButtonEvent];
}

#pragma mark - setter/getter methods
- (UIButton *)manageButton {
    if (_manageButton == nil) {
        
        _manageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _manageButton.hidden = YES;
        [_manageButton setExclusiveTouch:YES];
        [_manageButton setFrame:CGRectMake(0, 0, 40, 44)];
        [_manageButton addTarget:self action:@selector(manageButtonEvent) forControlEvents:UIControlEventTouchUpInside];
        [_manageButton setTitle:@"管理" forState:UIControlStateNormal];
        [_manageButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_manageButton setTitleColor:CSColorFromRGB(0xfea700) forState:UIControlStateHighlighted];
        _manageButton.titleLabel.font = [UIFont systemFontOfSize:16.];
    }
    return _manageButton;
}

- (SortBasicCollectionCellLayout *)collectionViewLayout
{
    if (_collectionViewLayout == nil) {
        _collectionViewLayout = [[SortBasicCollectionCellLayout alloc] init];
        //设置行距
        _collectionViewLayout.minimumLineSpacing = 15;
        //设置cell之间间距
        _collectionViewLayout.minimumInteritemSpacing = 15;
        _collectionViewLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
        _collectionViewLayout.datasource = self;
    }
    return _collectionViewLayout;
}

- (NSMutableArray *)commonArray {
    if (_commonArray == nil) {
        _commonArray = [NSMutableArray array];
    }
    return _commonArray;
}

- (NSMutableArray *)moreArray {
    if (_moreArray == nil) {
        _moreArray = [NSMutableArray array];
    }
    return _moreArray;
}

#pragma mark - Private methods
- (void)manageButtonEvent {
    if (!self.commonArray.count && !self.moreArray.count) { //如果没有内容为不可编辑
        return;
    }
    if (!self.isSelectManageButton) {
        [_manageButton setTitle:@"完成" forState:UIControlStateNormal];
        self.isSelectManageButton = YES;
        self.collectionViewLayout.commonCount = self.commonArray.count;
    }else{
        [_manageButton setTitle:@"管理" forState:UIControlStateNormal];
        self.isSelectManageButton = NO;
    }
    [self.sortCollectionView reloadData];
}

- (void)setUpTheViewFrame {
    self.view.backgroundColor = [UIColor whiteColor];
    //导航栏右边按钮
//    UIBarButtonItem *rightBarButtonitem = [[UIBarButtonItem alloc] initWithCustomView:self.manageButton];
//    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    negativeSpacer.width = -8;
//    self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightBarButtonitem];
    
    [self showRightImageWithImageName:@"close_pop"];
    
    //集合视图
    self.sortCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.collectionViewLayout];
    self.sortCollectionView.backgroundColor = [UIColor whiteColor];
    self.sortCollectionView.dataSource = self;
    self.sortCollectionView.delegate = self;
    [self.sortCollectionView registerClass:[SortCollectionViewCell class] forCellWithReuseIdentifier:kSortCollectionCell];
    [self.sortCollectionView registerClass:[SortCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kSortHeadReusableView];
    self.sortCollectionView.showsVerticalScrollIndicator = NO;
    self.sortCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-adaptNavagationBar);
    self.collectionViewLayout.itemSize = CGSizeMake((SCREENWIDTH - 75)/4, 40);
    [self.view addSubview:self.sortCollectionView];
    
}

#pragma mark - UICollectionViewDelegate methods
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return self.commonArray.count;
    }else{
        return self.moreArray.count  ; //1用于显示更多按钮
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSortCollectionCell forIndexPath:indexPath];
    if (indexPath.section == 0) {
        [cell setTheCellRightTopImageContent:self.isSelectManageButton ? TopRightImageDelete_Type : TopRightImageHide_Type];
        [cell setTheCellContent:self.commonArray[indexPath.item]];
    }else if (indexPath.section == 1 ) {
        [cell setTheCellRightTopImageContent:self.isSelectManageButton ? TopRightImageAdd_Type : TopRightImageHide_Type];
        if (indexPath.item < self.moreArray.count) {
            [cell setTheCellContent:self.moreArray[indexPath.item]];
        }else{
            [cell setTheCellContent:nil];
        }
    }
    return cell;
}
//头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return CGSizeMake(CGRectGetWidth(self.view.bounds), 50);
            break;
        case 1:
            return CGSizeMake(CGRectGetWidth(self.view.bounds), 50);
            break;
        default:
            return CGSizeMake(CGRectGetWidth(self.view.bounds),0.000001);
            break;
    }
    
}
//头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    SortCollectionReusableView *headerView = (SortCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kSortHeadReusableView forIndexPath:indexPath];
    headerView.isSelectManageButton = self.isSelectManageButton;
    headerView.sectionIndex = indexPath.section;
    return headerView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.item == self.moreArray.count) { //选取了更多按钮
        NSLog(@"selected more button");
        return;
    }
    if (!self.isSelectManageButton) {
        SortCollectionViewCell *cell = (SortCollectionViewCell *)[self.sortCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.item inSection:indexPath.section]];
        NSLog(@"selected %@",cell.gameModel.gameName);
        return;
    }
    id removeItem;
    if (indexPath.section == 0) {
        removeItem = self.commonArray[indexPath.item];
        [self.commonArray removeObjectAtIndex:indexPath.item];
    }
    if (indexPath.section == 1) {
        removeItem = self.moreArray[indexPath.item];
        [self.moreArray removeObjectAtIndex:indexPath.item];
    }
    SortCollectionViewCell *cell = (SortCollectionViewCell *)[self.sortCollectionView cellForItemAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        [self.moreArray insertObject:removeItem atIndex:0];
        [self.sortCollectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
        [cell setTheCellRightTopImageContent:TopRightImageAdd_Type]; //需要更新cell右上角的图片状态为添加
    }
    if (indexPath.section == 1) {
        [self.commonArray insertObject:removeItem atIndex:0];
        [self.sortCollectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
        [cell setTheCellRightTopImageContent:TopRightImageDelete_Type]; //需要更新cell右上角的图片状态为删除
    }
    self.collectionViewLayout.commonCount = self.commonArray.count;
    [self.sortCollectionView reloadData];
}

#pragma APHomePageSortBasicLayoutDataSource methods
- (BOOL)canMoveSortCollectionViewItemAtIndex {
    return self.isSelectManageButton;
}

- (void)moveSortCollectionViewItemDone {
    [self.sortCollectionView reloadData];
}

- (void)currentIndexPath:(NSIndexPath *)fromeIndexPath movedToIndexPath:(NSIndexPath *)toIndexPath {
    GameClassifyModel *tempModel = [self.commonArray objectAtIndex:fromeIndexPath.item];
    [self.commonArray removeObjectAtIndex:fromeIndexPath.item];
    [self.commonArray insertObject:tempModel atIndex:toIndexPath.item];
}

- (void)rightBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
